package com.portico.dept.solarch

@grails.plugins.springsecurity.Secured(['ROLE_SA_USER','ROLE_SA_ADMIN'])
class ProjectStatusController {


    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    static scaffold = true

    def index = {
        redirect(action: "list", params: params)
    }


    def addComment = { ProjectStatusComment comment ->
        if (comment.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'projectStatus.label', default: 'Project Status Comment'), comment.id])}"
            redirect(action:"show", id: comment.status.id)
        } else {
        render(view: "show", model:[projectStatusInstance: ProjectStatus.get(comment.status.id), projectStatusCommentInstance: comment])
        }

    }

    def ajaxShowComments = {
        def table =""
//        def projectStatus = new ProjectStatus()
//        projectStatus.properties = params
        ProjectStatus.get(params.id).comments.each {
            table += """<tr><td colspan=1>${it.commentType}</td><td colspan=6>${it.commentDescription}</td></tr>"""
        }
        render (text:"<table>"+table+"</table>")
    }

    def list = {
        withFormat {
            html {
                params.max = Math.min(params.max ? params.int('max') : 10, 100)
                [projectStatusInstanceList: ProjectStatus.list(params), projectStatusInstanceTotal: ProjectStatus.count()]
            }
            csv {
                String csvString = ProjectStatus.CSV_HEADER + System.properties.getProperty("line.separator");
                ProjectStatus.list(params).each {
                    csvString += it.toCSVString() + System.properties.getProperty("line.separator")
                }
                render(text: csvString, contentType: "text/csv", encoding: "UTF-8")

            }
        }

    }


    def listMaster = {

    }

    def create = {
        def projectStatusInstance = new ProjectStatus()
        projectStatusInstance.properties = params
        return [projectStatusInstance: projectStatusInstance]
    }

    def save = {
        def projectStatusInstance = new ProjectStatus(params)
        if (projectStatusInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'projectStatus.label', default: 'ProjectStatus'), projectStatusInstance.id])}"
//      redirect(action: "show", id: projectStatusInstance.id)
            render(text: 'Save Complete')
        }
        else {
            render(view: "create", model: [projectStatusInstance: projectStatusInstance])
        }
    }

    def removeComment = {
        def projectStatusComment = ProjectStatusComment.get(params.id)
        if (!projectStatusComment) {
            redirect controller: 'projectDashboard', action: 'list'
        }
        else {
            def projectStatus = projectStatusComment.status
            projectStatusComment.delete();
            redirect(action: 'show', id: projectStatus.id)
        }

    }

    def show = {
        def projectStatusInstance = ProjectStatus.get(params.id)
        def projectStatusComment =  new ProjectStatusComment()
        if (!projectStatusInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'projectStatus.label', default: 'ProjectStatus'), params.id])}"
            response.sendError(404, flash.message)
        }
        else {
            render ( view:"show", model:[projectStatusInstance: projectStatusInstance, projectStatusCommentInstance:projectStatusComment])
        }
    }

    def edit = {
        def projectStatusInstance = ProjectStatus.get(params.id)
        if (!projectStatusInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'projectStatus.label', default: 'ProjectStatus'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [projectStatusInstance: projectStatusInstance]
        }
    }

    def update = {
        def projectStatusInstance = ProjectStatus.get(params.id)
        if (projectStatusInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (projectStatusInstance.version > version) {

                    projectStatusInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'projectStatus.label', default: 'ProjectStatus')] as Object[], "Another user has updated this ProjectStatus while you were editing")
                    render(view: "edit", model: [projectStatusInstance: projectStatusInstance])
                    return
                }
            }
            projectStatusInstance.properties = params
            if (!projectStatusInstance.hasErrors() && projectStatusInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'projectStatus.label', default: 'ProjectStatus'), projectStatusInstance.id])}"
                redirect(action: "show", id: projectStatusInstance.id)
            }
            else {
                render(view: "edit", model: [projectStatusInstance: projectStatusInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'projectStatus.label', default: 'ProjectStatus'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def projectStatusInstance = ProjectStatus.get(params.id)
        if (projectStatusInstance) {
            try {
                projectStatusInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'projectStatus.label', default: 'ProjectStatus'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'projectStatus.label', default: 'ProjectStatus'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'projectStatus.label', default: 'ProjectStatus'), params.id])}"
            redirect(action: "list")
        }
    }
}
